<template>
  <div>
    <el-table :data="stockOutDetailList" style="width: 100%">
      <el-table-column label="序号" align="center">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="proName"/>
      <el-table-column label="数量" align="center" prop="num">
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.num"
            @change="handleInputChange(scope.row)"
          ></el-input>
        </template>
      </el-table-column>
      <el-table-column label="商品价格" align="center" prop="price"/>
      <el-table-column label="小计" align="center">
        <template slot-scope="scope">
          {{ (scope.row.num * scope.row.price).toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['xiaoshou:saleBackRequest:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--    <pagination-->
    <!--      v-show="total>0"-->
    <!--      :total="total"-->
    <!--      :page.sync="queryParams.pageNum"-->
    <!--      :limit.sync="queryParams.pageSize"-->
    <!--      @pagination="getList"-->
    <!--    />-->
  </div>
</template>

<script>
import request from "@/utils/request";
import {
  listSaleBackRequestDetail,
  getSaleBackRequestDetail,
  delSaleBackRequestDetail,
  addSaleBackRequestDetail,
  updateSaleBackRequestDetail
} from "@/api/xiaoshou/saleBackRequestDetail";

export default {
  name: "StockOutDetail",
  props: ["stockOutIdTo", "totalPriceTo", "saleBackRequestCodeTo", "totalNumTo"],
  data() {
    return {
      totalPrice: this.totalPriceTo,
      totalNum: this.totalNumTo,
      stockOutId: this.stockOutIdTo,
      saleBackRequestCode: this.saleBackRequestCodeTo,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售退货申请详情表格数据
      stockOutDetailList: [],
      //添加的退货详情
      saleBackRequestDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        stockOutId: this.stockOutId,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        saleBackRequestCode: [
          {required: true, message: "销售退货申请单编号不能为空", trigger: "blur"}
        ],
        proCode: [
          {required: true, message: "商品编号不能为空", trigger: "blur"}
        ],
        proNum: [
          {required: true, message: "数量不能为空", trigger: "blur"}
        ],
        createBy: [
          {required: true, message: "创建人不能为空", trigger: "blur"}
        ],
        createDate: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {

  },
  watch: {
    //改变出库单id
    stockOutIdTo() {
      this.stockOutId = this.stockOutIdTo;
      this.queryParams.stockOutId = this.stockOutId;
      this.getList();
    },
    totalPriceTo(newValue) {
      this.totalPrice = newValue;
    },
    totalNumTo(newValue) {
      this.totalNum = newValue;
    },
    saleBackRequestCodeTo(newValue) {
      this.saleBackRequestCode = newValue;
    },
  },
  mounted() {
    console.log(this.stockOutIdTo);
  },
  methods: {
    changeTotal() {
      let totalPrice = 0;
      let totalNum = 0;
      if (this.stockOutDetailList != null) {
        this.stockOutDetailList.forEach(item => {
          // 计算每个商品的价格
          item.itemPrice = (parseFloat(item.price) * parseInt(item.num)).toFixed(2); // 确保 price 和 num 是数字
          // 累计总价格
          totalPrice = totalPrice + parseFloat(item.itemPrice);// 使用 parseFloat 将 itemPrice 转换为数字进行累加
          totalNum = parseInt(totalNum) + parseInt(item.num);
        });
      } else {
        totalPrice = 0;
        totalNum = 0;
      }
      // Emit 事件将总价格传递给父组件
      this.$emit("update-total", totalPrice, totalNum);
    },
    handleDelete(id) {
      // 找到要删除的对象的索引
      const index = this.stockOutDetailList.findIndex(item => item.id === id);
      if (index !== -1) {
        // 使用 splice 删除对象
        this.stockOutDetailList.splice(index, 1);
        this.$notify({
          title: '成功',
          message: '已成功删除商品',
          type: 'success',
        });
      } else {
        this.$notify({
          title: '错误',
          message: '未找到要删除的商品',
          type: 'error',
        });
      }
      this.changeTotal();
    },
    handleInputChange(row) {
      if (isNaN(row.num) || row.num <= 0) {
        this.$notify({
          title: '警告',
          message: '请输入正整数',
          type: 'warning',
        });
        row.num = row.inputNum; // 恢复为num
      }
      if (row.inputNum < row.num) {
        this.$notify({
          title: '警告',
          message: `输入的数量不能大于` + row.inputNum,
          type: 'warning',
        });
        row.num = row.inputNum; // 恢复为num
      }
      this.changeTotal();
    },
    /** 查询销售退货申请详情列表 */
    getList() {
      this.loading = true;
      // request({
      //   url: "/xiaoshou/saleBackRequest/getStockOutDetailList",
      //   params: this.queryParams,
      //   methods: "get"
      // }).then(response => {
      //   this.stockOutDetailList = response.rows;
      //   //添加数量字段和小计字段
      //   if(this.stockOutDetailList!=null){
      //     this.stockOutDetailList.forEach(item => {
      //       item.inputNum = item.num;
      //       item.itemPrice = item.price*item.num;
      //     });
      //   }
      //   console.log(response);
      //   this.changeTotal();
      //   this.total = response.total;
      //   this.loading = false;
      // });
      request({
        url: "/store/stockOutDetail/getStockOutDetailUseList",
        params: this.queryParams,
        methods: "get"
      }).then(response => {
        this.stockOutDetailList = response.rows;
        //添加数量字段和小计字段
        if (this.stockOutDetailList != null) {
          this.stockOutDetailList.forEach(item => {
            item.inputNum = item.num;
            item.itemPrice = item.price * item.num;
          });
        }
        console.log(response);
        this.changeTotal();
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
      this.stockOutDetailList = null;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    //   /** 提交按钮 */
    addDetails() {
      this.stockOutDetailList.forEach(item => {
        const detail = {
          saleBackRequestCode: this.saleBackRequestCode,
          proCode: item.proCode,
          proNum: item.num,
          price: item.price,
          createBy: this.$store.getters.name,
        };
        this.saleBackRequestDetailList.push(detail);
      });

      request({
        url: "/xiaoshou/saleBackRequestDetail/addSaleBackRequestDetail",
        data: this.saleBackRequestDetailList,
        method: "post"
      }).then(res => {
        this.$modal.msgSuccess("新增成功");
      });
    },
  }
};
</script>
